package cn.itcast.web.sevlet;

import cn.itcast.domain.Student;
import cn.itcast.service.impl.BaseServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.PublicKey;
import java.util.ArrayList;

@WebServlet("/studentServlet")
public class StudentServlet extends BaseServlet{
    //登录
    public String login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Student stu = new BaseServiceImpl().findByStudentnameAndPassword(username, password);
        if(stu!=null){//如果有数据,说明账号密码正确
            request.getSession().setAttribute("username",stu.getUsername());
            //重定向
            return "redirect:studentServlet?method=loginSuccess";
        }else {//请求转发
            request.getSession().setAttribute("error","账户密码不正确");
            return "login.jsp";
        }
    }
    //登录成功
    public String loginSuccess(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.getWriter().print("登录成功，即将跳转");
        return "refresh:index.jsp";
    }
    //查看所有学生
    public String findAllStudent(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ArrayList<Student> students = new BaseServiceImpl().findAllStudent();

        if (students.size() != 0) {// 说明有学生信息
            request.getSession().setAttribute("studens",students);
            return "findStudent.jsp";
        }else {
            response.getWriter().print("查无学生信息，即将回退");
            return "refresh:index.jsp";
        }
    }
    //添加学生
    public String insertStudent(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        int resulf = new BaseServiceImpl().insertStudent(username, password);
        if(resulf==1){//添加成功
            response.getWriter().print("添加学生成功");
            return "refresh:index.jsp";
        }else {
            response.getWriter().print("添加学生失败,请重新添加");
            return "refresh:index.jsp";
        }
    }
    //删除学生
    public String deleteStudent(HttpServletRequest request, HttpServletResponse response)throws Exception{
        int delete_id = Integer.valueOf(request.getParameter("delete_id"));
        new BaseServiceImpl().deleteStudent(delete_id);
        return "redirect:/studentServlet?method=findAllStudent";
    }
    //查找需要修改的信息
    public String findUpdateStudent(HttpServletRequest request, HttpServletResponse response)throws Exception{
        int update_id = Integer.valueOf(request.getParameter("update_id"));
        Student stu = new BaseServiceImpl().findStudentById(update_id);
        request.setAttribute("student",stu);
        return "forward:update.jsp";
    }
    //修改
    public String updateStudent(HttpServletRequest request, HttpServletResponse response)throws Exception{
        int update_id = Integer.valueOf(request.getParameter("update_id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Student stu = new Student(update_id, username, password);
        new BaseServiceImpl().updateStudent(stu);
        return "redirect:/studentServlet?method=findAllStudent";
    }
}
